package com.xm.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wxm.servlet.BaseServlet;
import com.xm.dao.UserDaoImpl;
import com.xm.entity.Users;
import com.xm.uti.DBManager;
import com.xm.uti.QueryDataResult;
import com.xm.uti.Result;

/**
 * Servlet implementation class UserServletAdmin
 */
@WebServlet("/admin/UserServletAdmin")
public class UserServletAdmin extends BaseServlet {
	UserDaoImpl userDao = new UserDaoImpl();

	/**
	 * 获取用户
	 * 
	 * @param req
	 * @param resp 
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String userName = req.getParameter("userName");
		String userPwd = req.getParameter("userPwd");
		System.out.println(userName + userPwd);
		Users user = userDao.getUsers(userName, userPwd);
   
		String res = "";
		if (user != null) {
			res = Result.toClient(0, "登录成功");
			HttpSession session = req.getSession();
			session.setAttribute("username", userName);
			session.setAttribute("userurl", user.getUserUrl());
			System.out.println("用户名存session了");
		} else {
			 res= Result.toClient(1, "用户名或密码不正确");
		}
		PrintWriter out = resp.getWriter();
		out.print(res);
		out.flush();// 刷新
		out.close();// 关闭
	}

	/**
	 * 修改用户密码
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateUserPwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    // 1.接收客户端传来的参数
	    String mpass = req.getParameter("mpass");//获取原密码
	    String renewpass = req.getParameter("renewpass");//获取新密码
	     
	    //获取存在session中的值 username
	    HttpSession session = req.getSession();
	    String username = (String)session.getAttribute("username");
	    System.out.println("原密码"+mpass);
	    System.out.println("新密码"+renewpass);
	    
	    //调用dao层方法 根据name返回对象
	    Users users = userDao.getUsers(username,mpass);
	    System.out.println(users);
	    System.out.println("");
	    PrintWriter out = resp.getWriter(); // 获取输出对象
	    if(mpass.equals(users.getUserPwd())) {
	      boolean res = userDao.updateUsers(username,renewpass);
	      // 3.判断结果，实现跳转
	      if(res) {
	        out.write(Result.toClient(0, "修改成功"));
	      }else {
	        out.write(Result.toClient(1, "修改失败"));
	      }
	    }else {
	      out.write(Result.toClient(1, "原密码不对"));
	    }
	    out.flush();  //刷新
	    out.close();  //关闭
	  }
	/**
	 * 修改用户头像
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateUserUrl(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.获取参数
		String userUrl = req.getParameter("userUrl");
		HttpSession session = req.getSession();
		String id = (String)session.getAttribute("id");
		Users users = userDao.Users(id);
		PrintWriter out = resp.getWriter(); 
		if(userUrl.equals(users.getUserUrl())) {
			boolean res = userDao.updatePhose(userUrl, id);
			if(res) {
				out.write(Result.toClient(0, "修改成功"));
			}else {
				out.write(Result.toClient(1, "修改失败"));
			}
		}else {
			out.write(Result.toClient(1, "填全以上内容"));
		}
		out.flush();// 刷新
		out.close();// 关闭
	}
	/**
	 * 退出
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doLogout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		session.removeAttribute("username");
		session.removeAttribute("userurl");
		System.out.println("用户名从session移除了");
		String res = Result.toClient(1, "退出成功");
		PrintWriter out = resp.getWriter(); 
		out.print(res);
		out.flush();// 刷新
		out.close();// 关闭
	}
}
